/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author tea milandi
 */
public class PengirimanBarang {

    private String kodePengiriman;
    private String waktuPengiriman;
    private String kodePemesanan;
    private String username;
    private double jumlahBarang;
    private String idBarang;
    private double hargaBarang;
    private String status;
    String jdbcUrl = "jdbc:oracle:thin:@172.23.9.185:1521:orcl";
    String userid = "mhs115314008"; // gantikan dengan username anda
    String pass = "mhs115314008"; // gantikan dengan password anda

    public String getKodePengiriman() throws SQLException {
        Connection connection = null;
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        ds.setPassword(pass);
        ds.setUser(userid);
        connection = (Connection) ds.getConnection();
        Statement statement = connection.createStatement();
        SimpleDateFormat sdf1 = new SimpleDateFormat("dMyy");
        Date d = new Date();
        String finalDate = sdf1.format(d);
        String k1 = generateKodePengiriman(finalDate);
        String code = null;
        String kod = k1.substring(0, 6);
        int a = 0;
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery("select kodePengiriman from pengiriman_asmart where kodePengiriman like '" + kod + "%'");
        while (rs.next()) {
            String sementara = rs.getString("kodePengiriman");
            String s2 = sementara.substring(6);
            a = Integer.parseInt(s2);
            a++;
        }
        return this.kodePengiriman = kod + String.valueOf(a);
    }

    public String getWaktuPengiriman() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d-M-y");
        this.waktuPengiriman = simpleDateFormat.format(date);
        return waktuPengiriman;
    }

    public String getKodePemesanan() {
        return kodePemesanan;
    }

    public void setKodePemesanan(String kodePemesanan) {
        this.kodePemesanan = kodePemesanan;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public double getJumlahBarang() {
        return jumlahBarang;
    }

    public void setJumlahBarang(double jumlahBarang) {
        this.jumlahBarang = jumlahBarang;
    }

    public String getIdBarang() {
        return idBarang;
    }

    public void setIdBarang(String idBarang) {
        this.idBarang = idBarang;
    }

    public double getHargaBarang() {
        return hargaBarang;
    }

    public void setHargaBarang(double hargaBarang) {
        this.hargaBarang = hargaBarang;
    }

    public void insertPengiriman() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeQuery("insert into pengiriman_asmart values ('"
                    + getKodePengiriman() + "','"
                    + getWaktuPengiriman() + "','"
                    + getKodePemesanan() + "','"
                    + getUsername() + "',"
                    + getJumlahBarang() + ",'"
                    + getIdBarang() + "',"
                    + getHargaBarang() + ")");
            StokBarang barangPusat = new StokBarang();
            barangPusat.setIdBarang(getIdBarang());
            barangPusat.setJumlahBarang(getJumlahBarang());
            barangPusat.kurangBarang(barangPusat);
        } catch (SQLException e) {
            System.out.println("error :" + e.getMessage());
        }
    }

    public String generateKodePengiriman(String date) {
        String kode;
        kode = date + "0";
        return kode;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }

    public void updateJumlahBarang(String jum, String kodepesan) throws SQLException {
        Connection connection = null;
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        ds.setPassword(pass);
        ds.setUser(userid);
        connection = (Connection) ds.getConnection();
        Statement statement = connection.createStatement();
        statement.executeQuery("update pemesanan_asmart set jumlahbarang='" + jum + "'"
                + " where kodepemesanan='" + kodepesan + "'");
    }
}
